var TopMain;
(function(TopMain) {
	function FormTableCtrl($scope, $routeParams, GenericResource) {
		BaseCrud.CrudCtrl($scope, $routeParams, GenericResource);    
	    $scope.swipingCard =function(event,cardId){
	       if(event.keyCode !== 13){
	           return;//不是enter键返回。
	       }
	        var cardSearchCondition = {example:{id: cardId}};
	       var successCallback=function(result){
	          if(result.length ===0){
	              alert("查询卡信息失败，请确认是否存在此卡！");
	          } else{
	              if($scope.currentItem.position === undefined){
	                  $scope.currentItem.position={};
	              }
	              $scope.currentItem.position.id=result[0].position.id;
	          }
	       };
	       GenericResource.post({"modelType": "MemberCard"}, cardSearchCondition,successCallback);
	    };
	    
	    $scope.addMaterial = function(childrenItem,param) {
	        if (childrenItem === undefined || param === undefined) {            
	            return;
	        }
	        var successCallback = function(result) {
	            if(result.length === 1 ){
	                childrenItem.push({material: result[0],quantity: 1});
	            }            
	        };       
	        var materialSearchCondition = {example:{id: param}};
	        GenericResource.post({"modelType": "Material"}, materialSearchCondition,successCallback);
	    };    
	    $scope.$watch('currentItem.orderItems', function(newVal, oldVal) {
	        if (newVal !== oldVal && newVal !==undefined) {
	            var sum=0;
	            for (var i in newVal) {
	                var item=newVal[i];
	                if(!isNaN(item.material.price) && !isNaN(item.quantity)){
	                    sum=sum+item.material.price*item.quantity;
	                }
	            }
	            $scope.currentItem.sum=sum;
	        }
	    }, true);    
	}
	
	function MenuCtrl($scope,$resource,GenericResource) {
		$scope.menuTree=$resource(clientRoot+'/js/config/menu.json',{},{'queryAll': {method: 'GET', isArray: true}}).queryAll(); 
		
	    $scope.logout = function() {
	        GenericResource.query({"path": "profile", "method": "logout"});
	    };
	    $scope.settings = GenericResource.query({"path": "profile", "modelType": "forModel","method":"Menu"});
	}
	TopMain.FormTableCtrl=FormTableCtrl;
	//TopMain.MenuCtrl=MenuCtrl;
	var topMain =angular.module('topMain', ['ngResource']);
	topMain.controller('TopMain.MenuCtrl',function($scope,$resource,GenericResource) {
		MenuCtrl($scope,$resource,GenericResource);
	});
	//hawtioPluginLoader.addModule("topMain");
})(TopMain || (TopMain = {}));